package cn.nexd.collector.bean;

import android.bluetooth.BluetoothDevice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by codngfish on 16/6/1.
 */
class DefaultBeaconStoryStrategy extends BeaconCollectorStoryStrategy {
    private Map<String, BeaconScannerResult> map = new HashMap<>();

    //    @Override
    //    public void process(BluetoothDevice device, int rssi, byte[] scanRecord, long timestamp) {
    //
    //        int startByte = 2;
    //
    //        while (startByte <= 5) {
    //            //Identifies an iBeacon
    //            //Identifies correct data length
    //            if (((int) scanRecord[startByte + 2] & 0xff) == 0x02 && ((int) scanRecord[startByte + 3] & 0xff) == 0x15) {
    //
    //                break;
    //            }
    //            startByte++;
    //        }
    //    }


    @Override
    public List<BeaconScannerResult> getBeaconScanerResults() {
        List<BeaconScannerResult> beaconScannerResults = new ArrayList<>();
        for (Map.Entry<String, BeaconScannerResult> entry : map.entrySet()) {
            beaconScannerResults.add(entry.getValue());
        }
        clear();
        return beaconScannerResults;
    }

    @Override
    void handlerData(BluetoothDevice device, int rssi, byte[] scanRecord, long timestamp, int startByte) {
        final String mac = device.getAddress();
        if (map.containsKey(mac)) {
            // 包括了,更新
            BeaconScannerResult beaconScannerResult = map.get(mac);
            beaconScannerResult.setRssi((beaconScannerResult.getRssi() + rssi) / 2);
            //            beaconScannerResult.setBeaconName(device.getName());
            //            beaconScannerResult.setBeaconAddress(device.getAddress());
            //            beaconScannerResult.setStartRssi(rssi);
            beaconScannerResult.setEndRssi(rssi);
            beaconScannerResult.setEndTimestamp(timestamp);
            //            beaconScannerResult.setStartTimestamp(timestamp);
            //            beaconScannerResult.setMajor(beaconManufacturerData.getMajor());
            //            beaconScannerResult.setMinor(beaconManufacturerData.getMinor());
            beaconScannerResult.setmScanRecord(scanRecord);
            beaconScannerResult.setTimestamp(timestamp);
            //            beaconScannerResult.setUUID(beaconManufacturerData.getUUID());
            map.put(mac, beaconScannerResult);
        } else {
            // 不包括,添加
            BeaconScannerResult beaconScannerResult = new BeaconScannerResult();
            beaconScannerResult.setRssi(rssi);
            beaconScannerResult.setBeaconName(device.getName());
            beaconScannerResult.setBeaconAddress(device.getAddress());
            beaconScannerResult.setStartRssi(rssi);
            beaconScannerResult.setEndRssi(rssi);
            beaconScannerResult.setEndTimestamp(timestamp);
            beaconScannerResult.setStartTimestamp(timestamp);
            //            beaconScannerResult.setMajor(IBeaconUtils.getIntFrom2ByteArray(Arrays.copyOfRange(adRecordStore.getRecord(AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA).getData(), 20, 22)));
            beaconScannerResult.setMajor(getMajor(startByte, scanRecord));
            //                    beaconScannerResult.setMinor(IBeaconUtils.getIntFrom2ByteArray(Arrays.copyOfRange(adRecordStore.getRecord(AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA).getData(), 22, 24)));
            beaconScannerResult.setMinor(getMinor(startByte, scanRecord));
            beaconScannerResult.setmScanRecord(scanRecord);
            beaconScannerResult.setTimestamp(timestamp);
            //                    beaconScannerResult.setUUID(IBeaconUtils.calculateUuidString(Arrays.copyOfRange(adRecordStore.getRecord(AdRecord.TYPE_MANUFACTURER_SPECIFIC_DATA).getData(), 4, 20)));
            beaconScannerResult.setUUID(getUUID(startByte, scanRecord));
            map.put(mac, beaconScannerResult);
        }
    }

    @Override
    public void clear() {
        map.clear();
    }


}
